package org.ielse.frame.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

public class LoadingView extends View {

	private final Paint paint = new Paint();
	private final RectF rectF = new RectF();

	private int mWidth;
	private int mHeight;
	private int centerX;
	private int centerY;
	private int mRadius;
	private int ringRadius;
	private int ringSize;

	public LoadingView(Context context) {
		this(context, null);
	}

	public LoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int size = MeasureSpec.getSize(widthMeasureSpec);
		setMeasuredDimension(size, size);
	}

	@Deprecated @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mWidth = w;
		mHeight = h;
		mRadius = mWidth / 2;
		centerX = mWidth / 2;
		centerY = mHeight / 2;

		ringRadius = (int) ((mWidth - centerX) * 0.50f);
		ringSize = (int) ((mWidth - centerX) * 0.15f);

		rectF.left = centerX - ringRadius;
		rectF.right = centerY + ringRadius;
		rectF.top = centerY - ringRadius;
		rectF.bottom = centerX + ringRadius;

		Bitmap background = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_4444);
		Canvas c = new Canvas(background);
		paint.setAntiAlias(true);
		paint.setColor(0xbb000000);
		paint.setStyle(Style.FILL);
		c.drawCircle(centerX, centerY, mRadius, paint);
		setBackgroundDrawable(new BitmapDrawable(getResources(), background));
	}

	@Override protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setAntiAlias(true);

		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(ringSize);

		if (isDelay) {
			paint.setColor(0xffcdcdcd);
			canvas.drawArc(rectF, -90, delayPercent, false, paint);
		}
		else {
			paint.setColor(0xffcdcdcd);
			canvas.drawArc(rectF, -90 + progress, 360 - progress + 1, false, paint);
			paint.setColor(0xffffffff);
			canvas.drawArc(rectF, -90, progress + 1, false, paint);
		}
	}

	private int delay = 2000;
	private int delayPercent;
	private int progress;

	private boolean isDelay = true;
	ValueAnimator animDelay;
	ValueAnimator animDown;
	public void startDelay() {
		isDelay = true;
		setVisibility(View.VISIBLE);
		 animDelay = ValueAnimator.ofInt(0, 360);
		animDelay.setDuration(delay);
		animDelay.start();
		animDelay.addUpdateListener(new AnimatorUpdateListener() {
			@Override public void onAnimationUpdate(ValueAnimator animation) {
				delayPercent = (Integer) animation.getAnimatedValue();
				invalidate();
			}
		});

		animDelay.addListener(new AnimatorListenerAdapter() {
			@Override public void onAnimationEnd(Animator animation) {
				isDelay = false;

				startDownLoadTest();
			}
		});
	}

	public void progress(int progress) {

		invalidate();
	}

	public void startDownLoadTest() {
		animDown = ValueAnimator.ofInt(0, 360);
		animDown.setDuration(6000);
		animDown.start();
		animDown.addUpdateListener(new AnimatorUpdateListener() {
			@Override public void onAnimationUpdate(ValueAnimator animation) {
				progress = (Integer) animation.getAnimatedValue();
				progress(progress);
			}
		});

		animDown.addListener(new AnimatorListenerAdapter() {
			@Override public void onAnimationEnd(Animator animation) {
				setVisibility(View.GONE);
			}
		});
	}
}
